<!DOCTYPE html>
<html>
<head>
    <title>BigCorn 后台管理系统</title>
    <% include("../layout/cssPart.html"){} %>
</head>
<body>

<!-- 页面加载loading -->
<% include("../layout/loading.html"){} %>

<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header">
            <span class="layui-breadcrumb pull-right">
          <a href="#!home_console">首页</a>
          <a><cite>用户管理</cite></a>
        </span>
        </div>
        <div class="layui-card-body">
            <div class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline" style="width: 120px;">
                        <select id="sltKey">
                            <option value="">-搜索条件-</option>
                            <option value="id">ID</option>
                            <option value="userName">账号</option>
                            <option value="realName">用户名</option>
                            <option value="mobilePhone">手机号</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input id="edtSearch" class="layui-input" type="text" placeholder="输入关键字"/>
                    </div>
                    <div class="layui-inline">
                        <button id="btnSearch" class="layui-btn icon-btn"><i class="layui-icon">&#xe615;</i>搜索</button>
                        <button id="btnAdd" class="layui-btn icon-btn" lay-tips="新用户密码为123456"><i class="layui-icon">&#xe654;</i>添加</button>
                        <button id="btnExp" class="layui-btn icon-btn" ><i class="layui-icon">&#xe67d;</i>导出</button>
                    </div>
                </div>
            </div>

            <table class="layui-table" id="userTable" lay-filter="userTable"></table>
        </div>
    </div>
</div>
<!-- 表格操作列 -->
<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-xs" lay-event="reset">重置密码</a>
</script>

<!-- 表格状态列 -->
<script type="text/html" id="tbaleState">
    <input type="checkbox" lay-filter="status" value="{{d.id}}" lay-skin="switch" lay-text="正常|锁定"
           {{d.status==0?'checked':''}}/>
</script>

<!-- js部分 -->
<% include("../layout/jsPart.html"){} %>
<script>
    layui.use(['layer', 'form', 'table', 'util', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var util = layui.util;
        var admin = layui.admin;

        // 渲染表格
        var ins1 = table.render({
            elem: '#userTable',
            url: 'user/list',
            page: true,
            cols: [[
                {type: 'numbers'},
                {type: 'checkbox'},
                {field: 'userName', sort: true, title: '账号'},
                {field: 'realName', sort: true, title: '用户名'},
                {field: 'mobilePhone', sort: true, title: '手机号'},
                {field: 'sex', sort: true, title: '性别'},
                {
                    sort: true, templet: function (d) {
                    return util.toDateString(d.createDate);
                }, title: '创建时间'
                },
                {field: 'status', sort: true, templet: '#tbaleState', title: '状态'},
                {align: 'center', toolbar: '#tableBar', title: '操作'}
            ]]
        });

        // 添加按钮点击事件
        $('#btnAdd').click(function () {
            showEditModel();
        });

        // 搜索按钮点击事件
        $('#btnSearch').click(function () {
            var key = $('#sltKey').val();
            var value = $('#edtSearch').val();
            table.reload('userTable', {where: {searchKey: key, searchValue: value}});
        });

        // 工具条点击事件
        table.on('tool(userTable)', function (obj) {
            var data = obj.data;
            var layEvent = obj.event;

            if (layEvent === 'edit') { // 修改
                showEditModel(data);
            } else if (layEvent === 'reset') { // 重置密码
                resetPsw(obj.data.id);
            }
        });

        // 修改user状态
        form.on('switch(status)', function (obj) {
            layer.load(2);
            $.post('user/updateState', {
                userId: obj.elem.value,
                state: obj.elem.checked ? 0 : 1
            }, function (data) {
                layer.closeAll('loading');
                if (data.code == 200) {
                    layer.msg(data.msg, {icon: 1});
                } else {
                    layer.msg(data.msg, {icon: 2});
                    $(obj.elem).prop('checked', !obj.elem.checked);
                    form.render('checkbox');
                }
            });
        });

        // 导出excel
        $('#btnExp').click(function () {
            var checkRows = table.checkStatus('userTable');
            if (checkRows.data.length == 0) {
                layer.msg('请选择要导出的数据', {icon: 2});
            } else {
                table.exportFile(ins1.config.id, checkRows.data, 'xls');
            }
        });

        // 重置密码
        function resetPsw(userId) {
            top.layer.confirm('确定重置此用户的密码吗？', function (i) {
                top.layer.close(i);
                layer.load(2);
                $.post('user/restPsw', {
                    userId: userId
                }, function (data) {
                    layer.closeAll('loading');
                    if (data.code == 200) {
                        layer.msg(data.msg, {icon: 1});
                    } else {
                        layer.msg(data.msg, {icon: 2});
                    }
                });
            });
        }

        // 显示表单弹窗
        function showEditModel(data) {
            admin.putTempData('t_user', data);
            admin.putTempData('formOk', false);
            top.layui.admin.open({
                type: 2,
                title: data ? '修改用户' : '添加用户',
                area: ['420px', '520px'],
                content: 'system/user/editForm',
                end: function () {
                    admin.getTempData('formOk') && table.reload('userTable');  // 成功刷新表格
                }
            });
        }
    });

</script>
</body>

</html>